package com.enjoyor.soa.traffic.core.efoms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.efoms.domain.PingConnectHis;
import com.enjoyor.soa.traffic.core.efoms.domain.PingConnectReal;
import com.enjoyor.soa.traffic.core.efoms.mapper.IPingConnectHisMapper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IPingConnectRealMapper;
import com.enjoyor.soa.traffic.core.efoms.service.IPingConnectRealService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.ReflectHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;

@Service
@Transactional
public class PingConnectRealService implements IPingConnectRealService {

    @Autowired
    private IPingConnectRealMapper pingConnectRealMapper;

    @Autowired
    private IPingConnectHisMapper pingConnectHisMapper;

    @Autowired
    private IPubLogMapper pubLogMapper;

    @Override
    public PingConnectReal selectPingConnectRealById(String pingRealId){
        return pingConnectRealMapper.selectById(pingRealId);
    }
    
    @Override
	public List<PingConnectReal> selectPingConnectRealList(String pingRealId, String devCategoryCode,
			String devTypeCode, String devId, String devName, String creatStartTime, String creatEndTime) {
    	try {
	    	Map<String,Object> paramT = new HashMap<String ,Object>();
	    	paramT.put("pingRealId", pingRealId);
	    	paramT.put("devCategoryCode", devCategoryCode);
	    	paramT.put("devTypeCode", devTypeCode);
	    	paramT.put("devId", devId);
	    	paramT.put("devName", devName);
	    	paramT.put("creatStartTime", creatStartTime);
	    	paramT.put("creatEndTime", creatEndTime);
	    	return pingConnectRealMapper.selectPingConnectRealList(paramT);
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    //查询是否存在类别、类型、设备相同的数据，存在返回第一条数据
    public PingConnectReal selectPingConnectReal(String devCategoryCode, String devTypeCode, String devId) {
    	try {
    		PingConnectReal pingConnectReal = null;
    		List<PingConnectReal> pingConnectRealList = this.selectPingConnectRealList(null, devCategoryCode, 
    				devTypeCode, devId, null, null, null);
	    	if(null != pingConnectRealList && pingConnectRealList.size() > 0) {
	    		pingConnectReal = pingConnectRealList.get(0);
	    	}
	    	return pingConnectReal;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    
    @Override
    public String insertPingConnectReal(PingConnectReal pingConnectReal, String personName){
    	try {
    		String pingRealId = "";
    		String devCategoryCode = pingConnectReal.getDevCategoryCode();
    		String devTypeCode = pingConnectReal.getDevTypeCode();
    		String devId = pingConnectReal.getDevId();
    		PingConnectReal selectPojo = this.selectPingConnectReal(devCategoryCode, devTypeCode, devId);
    		if(null == selectPojo) {
    			// 默认操作记录信息
    	        pingRealId = "PR" + TimeHelper.getUniqueString();
    	        pingConnectReal.setPingRealId(pingRealId);
    	        pingConnectReal.setCreatTime(TimeHelper.dateToLongString());
    	        pingConnectRealMapper.insert(pingConnectReal);
    		}else {
    			pingRealId = selectPojo.getPingRealId();
    			pingConnectReal.setPingRealId(pingRealId);
    	    	pingConnectReal.setCreatTime(TimeHelper.dateToLongString());
    	        pingConnectRealMapper.updateByIdSelective(pingConnectReal);
    		}
	        pubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
	                String.format("添加网络实时编号:{%s} 操作成功!", new Object[] { pingRealId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_MONITOR_MANAGEMENT.getMessage());
	        return pingRealId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
    
    //先判断是否存在，存在更新数据、不存在则新增；并且保存到历史表
    @Override
    public String insertPingConnectBatch(List<PingConnectReal> pingConnectRealList, String personName){
    	try {
    		String pingRealId = "";
	    	for(PingConnectReal pingConnectReal : pingConnectRealList) {
	    		String devCategoryCode = pingConnectReal.getDevCategoryCode();
	    		String devTypeCode = pingConnectReal.getDevTypeCode();
	    		String devId = pingConnectReal.getDevId();
	    		PingConnectReal selectPojo = this.selectPingConnectReal(devCategoryCode, devTypeCode, devId);
	    		if(null == selectPojo) {
	    			// 默认操作记录信息
	    	        pingRealId = "PR" + TimeHelper.getUniqueString();
	    	        pingConnectReal.setPingRealId(pingRealId);
	    	        pingConnectReal.setCreatTime(TimeHelper.dateToLongString());
	    	        pingConnectRealMapper.insert(pingConnectReal);
	    		}else {
	    			pingRealId = selectPojo.getPingRealId();
	    			pingConnectReal.setPingRealId(pingRealId);
	    	    	pingConnectReal.setCreatTime(TimeHelper.dateToLongString());
	    	        pingConnectRealMapper.updateByIdSelective(pingConnectReal);
	    		}
	    		//网络连接数据保存到历史表
	    		PingConnectHis pingConnectHis = ReflectHelper.beanToBean(pingConnectReal, PingConnectHis.class);
	    		String pingHisId = "PH" + TimeHelper.getUniqueString();
	    		pingConnectHis.setPingHisId(pingHisId);
	    		pingConnectHisMapper.insert(pingConnectHis);
	    	}
	        pubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
	                String.format("批量添加网络实时数据:{%s} 操作成功!", new Object[] { pingRealId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_MONITOR_MANAGEMENT.getMessage());
	        return pingRealId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }

    @Override
    public String updatePingConnectReal(PingConnectReal pingConnectReal, String personName){
    	try {
	    	String pingRealId = pingConnectReal.getPingRealId();
	    	pingConnectReal.setCreatTime(TimeHelper.dateToLongString());
	        pingConnectRealMapper.updateByIdSelective(pingConnectReal);
	        pubLogMapper.saveNoramlLogInfo(personName,null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
	                String.format("修改网络实时编号:{%s} 操作成功!", new Object[] { pingRealId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_MONITOR_MANAGEMENT.getMessage());
	        return pingRealId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }

    @Override
    public String deletePingConnectReal(String pingRealId, String personName){
    	try {
    		pingConnectRealMapper.deleteById(pingRealId);
	        pubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
	                String.format("删除网络实时编号:{%s} 操作成功!", new Object[] { pingRealId }),
	                LogInfoDic.LOG_MODULAR_ENUM.EFOMS_MONITOR_MANAGEMENT.getMessage());
	        return pingRealId;
    	} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
    }
}
